<template>
  <div>
    <h1>商品分类表单</h1>
  </div>

  <el-button type="primary" @click="TosaveClassify2()">添加分类</el-button>

  <el-form :model="searchForm" :inline="true">
    <el-form-item label="分类名称">
      <el-input v-model="searchForm.keyWord" clearable></el-input>
    </el-form-item>
    <el-form-item label="分类状态" style="width: 300px">
      <el-select v-model="searchForm.typeStatus" placeholder="请选择" clearable>
        <el-option label="隐藏" :value="1" />
        <el-option label="显示" :value="0" />
      </el-select>
    </el-form-item>
    <el-button @click="loadList()">搜索</el-button>
  </el-form>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      default-expand-all
    >
      <el-table-column prop="id" label="id" sortable />
      <el-table-column prop="parentId" label="父id" sortable />
      <el-table-column prop="name" label="类型名称" sortable />
      <el-table-column prop="createBy" label="创建人" sortable />
      <el-table-column prop="typeStatus" label="类型状态" sortable>
        <template #default="scope">
          <el-tag v-if="scope.row.typeStatus == 0">显示</el-tag>
          <el-tag v-if="scope.row.typeStatus == 1">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="typeImage" label="分类图片" sortable>
        <template #default="scope">
          <el-image
            :src="scope.row.typeImage"
            style="width: 70px; height: 70px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="typeTop" label="类型排序" sortable />
      <el-table-column label="操作" sortable>
        <template #default="scope">
          <el-button type="success" @click="TosaveClassify(scope.row)"
            >添加</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>

  <el-dialog v-model="dialogFormVisible" title="添加分类" width="500">
    <el-form :model="saveForm" :rules="rules" ref="ruleFormRef">
      <el-form-item
        label="分类名称"
        :label-width="formLabelWidth"
        prop="typeName"
      >
        <el-input v-model="saveForm.typeName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="分类图片" prop="typeImage">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:10010/team/index/uploadImage"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :headers="headers"
        >
          <img
            v-if="saveForm.typeImage"
            :src="saveForm.typeImage"
            class="avatar"
          />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="排序" :label-width="formLabelWidth" prop="typeTop">
        <el-input v-model="saveForm.typeTop" autocomplete="off" />
      </el-form-item>
      <el-form-item label="状态" prop="typeStatus">
        <el-radio-group v-model="saveForm.typeStatus">
          <el-radio :value="0">显示</el-radio>
          <el-radio :value="1">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          添加
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
const ruleFormRef = ref<FormInstance>();
import { treeList, saveType } from "@/api/api";
import { ElMessage } from "element-plus";
import { onMounted, ref } from "vue";
const tableData = ref([]);
const loadList = () => {
  treeList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("展示成功");
      tableData.value = res.data.data;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

onMounted(() => {
  loadList();
});
const dialogTableVisible = ref(false);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const saveForm = ref({
  typeTop: "",
  typeStatus: null,
  typeImage: "",
  typePid: null,
  typeName: "",
});
const searchForm = ref({
  keyWord: "",
  typeStatus: null,
});
const TosaveClassify = (row: any) => {
  saveForm.value = {
    typeTop: "",
    typeStatus: null,
    typeImage: "",
    typePid: null,
    typeName: "",
  };
  saveForm.value.typePid = row.id;
  console.log("1111:" + saveForm.value.typePid);
  dialogFormVisible.value = true;
};

import type { UploadProps } from "element-plus";

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  saveForm.value.typeImage = response.data;
};

const headers = ref({
  token: sessionStorage.getItem("token"),
});

const rules = ref({
  typeName: [{ required: true, message: "请输入类型名称", trigger: "blur" }],
  typeTop: [{ required: true, message: "请输入类型排序", trigger: "blur" }],
  typeStatus: [{ required: true, message: "请选择类型状态", trigger: "blur" }],
  typeImage: [{ required: true, message: "请输入类型图片", trigger: "change" }],
});
import type { FormInstance, FormRules } from "element-plus";
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");

      saveType(saveForm.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          loadList();
          window.onload;
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const TosaveClassify2 = () => {
  saveForm.value = {
    typeName: "",
    typeTop: "",
    typeStatus: null,
    typeImage: "",
    typePid: null,
  };
  dialogFormVisible.value = true;
};
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
